"""
Case Type   : ustore_autovacuum_io_limits
Case Name   : autovacuum_io_limits参数验证
Create At   : 2022/01/14
Owner       : zhao-pengcheng-g
Description :
    1.autovacuum_io_limits有效值验证
    2.autovacuum_io_limits无效值验证
Expect      :
    1.设置成功
    2.合理报错
History     :
"""
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Ustore_autovacuum_io_limits(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Ustore_Autovacuum_Case0004 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.config_item = 'autovacuum_io_limits'
        self.show_cmd = f'show autovacuum_io_limits;'
        show_result = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(show_result)
        self.check_default = show_result.splitlines()[-2].strip()

    def test_autovacuum_io_limits(self):
        text = '---step1:autovacuum_io_limits有效值验证，expect:设置成功---'
        self.logger.info(text)
        guc_result_1 = self.sh_primy.execute_gsguc('reload', 
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = 0')
        self.assertTrue(guc_result_1, '执行失败:' + text)
        show_msg_1 = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(show_msg_1)
        self.assertIn('0', show_msg_1, '执行失败:' + text)
        guc_result_2 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = 124545121')
        self.assertTrue(guc_result_2, '执行失败:' + text)
        show_msg_2 = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(show_msg_2)
        self.assertIn('124545121', show_msg_2, '执行失败:' + text)
        guc_result_3 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = 1073741823')
        self.assertTrue(guc_result_3, '执行失败:' + text)
        show_msg_3 = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(show_msg_3)
        self.assertIn('1073741823', show_msg_3, '执行失败:' + text)
        guc_result_4 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = -1')
        self.assertTrue(guc_result_4, '执行失败:' + text)
        show_msg_4 = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(show_msg_4)
        self.assertIn('-1', show_msg_4, '执行失败:' + text)

        text = '---step2:autovacuum_io_limits无效值验证，expect:合理报错---'
        self.logger.info(text)
        guc_result_5 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = -123')
        self.assertFalse(guc_result_5, '执行失败:' + text)
        guc_result_6 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = 1073741824')
        self.assertFalse(guc_result_6, '执行失败:' + text)
        guc_result_7 = self.sh_primy.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.config_item} = abcd*&')
        self.assertFalse(guc_result_7, '执行失败:' + text)
        guc_result_8 = self.sh_primy.execute_gsguc('reload',
                                       self.constant.GSGUC_SUCCESS_MSG,
                                       f'{self.config_item} = 241.486')
        self.assertFalse(guc_result_8, '执行失败:' + text)


    def tearDown(self):
        text = '---step3:恢复参数，expect:成功---'
        self.logger.info(text)
        check_res = self.sh_primy.execut_db_sql(self.show_cmd)
        self.logger.info(check_res)
        current = check_res.splitlines()[-2].strip()
        if self.check_default != current:
            guc_result = self.sh_primy.execute_gsguc('reload',
                            self.constant.GSGUC_SUCCESS_MSG,
                            f'{self.config_item} = {self.check_default}')
            show_msg = self.sh_primy.execut_db_sql(self.show_cmd)
            self.logger.info(show_msg)
            self.assertTrue(guc_result, '执行失败:' + text)
            self.assertIn(self.check_default, show_msg, '执行失败:' + text)
        self.logger.info(
            '--Opengauss_Function_Ustore_Autovacuum_Case0004 finish---')